//
//  ShellTotalRewardView.m
//  ShellWallert
//
//  Created by lvzhao on 2019/10/3.
//  Copyright © 2019 VV. All rights reserved.
//

#import "ShellTotalRewardView.h"
#import "ShellHomeViewModel.h"
#import "LZBaseTableView.h"
#import "ShellTotalRewardCell.h"
#import "ShellTotalRewardModel.h"



@interface  ShellTotalRewardView()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) ShellHomeViewModel *viewModel;

//总奖励
@property (nonatomic,strong) UILabel *rewardNumLabel;

@property (nonatomic,strong) LZBaseTableView *tableView;

@end

@implementation ShellTotalRewardView

- (instancetype)initViewModel:(LZBaseViewModel *)viewModel
{
    self = [super init];
    if (self) {
        
        self.cornerRadius = 6;
        self.backgroundColor = UIColorHex(0x08233F);
        self.viewModel = (ShellHomeViewModel *)viewModel;
        [self setupView];
        
    }
    return self;
}


#pragma mark - setupView
- (void)setupView{
    
    ///!!!:总奖励
    UILabel *rewardLab = [UILabel lz_labelWithText:[NSString stringWithFormat:@"%@(USDT)", LZLocalizedString(@"矿池总奖励")] fontSize:k_textFont(12) color:UIColorHex(0x156097)];
    [self addSubview:rewardLab];
    [rewardLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(20);
        make.width.mas_equalTo(240);
        make.height.mas_equalTo(17);
    }];
    
    [self addSubview:self.rewardNumLabel];
    [self.rewardNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.centerY.mas_equalTo(rewardLab.mas_centerY);
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(22);
    }];
    self.rewardNumLabel.text = @"0";
    
    UIImageView *lineView = [[UIImageView alloc] init];
    lineView.image = k_imageName(@"icon_xuxian");
    [self addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(48);
        make.height.mas_equalTo(1);
    }];
    
    [self addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(49);
        make.bottom.mas_equalTo(0);
    }];
    
    @weakify(self);
    //翻译
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:k_APPLanguageWillChangeNotificationName object:nil] subscribeNext:^(NSNotification * _Nullable x) {
        @strongify(self);
        rewardLab.text = [NSString stringWithFormat:@"%@(USDT)", LZLocalizedString(@"矿池总奖励")];
        [self.tableView reloadData];
    }];
    
    [self.viewModel.reloadSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        self.rewardNumLabel.text = [NSString stringWithFormat:@"%f",self.viewModel.accountModel.miningAward].saveDecimal;
        
        if([x isKindOfClass:[ShellTotalRewardModel class]]){
            ShellTotalRewardModel *rewardModel = x;
            [self.viewModel.dataArray insertObject:rewardModel atIndex:0];
            NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
            dispatch_async(dispatch_get_main_queue(), ^{
                @strongify(self)
                [self.tableView beginUpdates];
                [self.tableView insertRowsAtIndexPaths:@[newIndexPath]  withRowAnimation:UITableViewRowAnimationTop];
                [self.tableView endUpdates];
            });
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
            });
            
        } else {
            [self.tableView reloadData];
        }
    }];
    self.rewardNumLabel.text = @"0";

    
    
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.viewModel.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ShellTotalRewardCell *rewardCell = [tableView dequeueReusableCellWithIdentifier:@"ShellTotalRewardCell"];
    rewardCell.modelObject = self.viewModel.dataArray[indexPath.row];
    return rewardCell;
}


#pragma mark - get && set
- (LZBaseTableView *)tableView{
    if(!_tableView){
        _tableView = [[LZBaseTableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.scrollEnabled = NO;
        _tableView.backgroundColor = UIColorHex(0x08233F);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ShellTotalRewardCell class] forCellReuseIdentifier:@"ShellTotalRewardCell"];
        _tableView.rowHeight = 34;
    }
    return _tableView;
}



//总奖励
- (UILabel *)rewardNumLabel{
    if(!_rewardNumLabel){
        _rewardNumLabel = [UILabel lz_labelWithText:@"" fontSize:k_textFont(16) color:UIColorHex(0xFFFFFF)];
        [_rewardNumLabel lz_rightAlignment];
    }
    return _rewardNumLabel;
}



@end
